//敌人
function Enemy({
    width,
    height,
    x,
    y,
    img,
    speed
}) {
    Role.call(this, {
        width,
        height,
        x,
        y
    });
    this.img = img;
    this.speed = speed;
    //创建DOM元素
    this.createDom();
}
//原型链继承
Enemy.prototype = Object.create(Role.prototype);
//创建DOM元素
Enemy.prototype.createDom = function () {
    //创建DOM元素
    this.element = document.createElement("div");
    //设置CSS属性
    this.element.style.width = this.width + "px";
    this.element.style.height = this.height + "px";
    this.element.style.background = `url(${this.img})`;
    this.element.style.position = "absolute";
    this.element.style.left = this.x + "px";
    this.element.style.top = this.y + "px";
    //将DOM节点添加到地图中显示
    Map.addRole(this);
};
//重写Go
Enemy.prototype.go = function () {
    if (!this.isAlive)
        return;
    //在移动过程中Y坐标是更改的
    this.y += this.speed;
    //判断是否超出地图的范围
    if (this.y >= Map.height) {
        this.isAlive = false; //超出地图范围则令其从节点上删除
        this.element.parentNode.removeChild(this.element);
        return;
    }
    //修改css
    this.element.style.top = this.y + "px";
};
//创建敌机的工厂
const EnemyFactory = {
    createEnemy: function (type) {
        if (type === "small") {
            return new Enemy({
                width: 34,
                height: 24,
                x: random(0, Map.width - 34),
                y: 0,
                img: "images/small_fly.png",
                speed: 4
            });
        }
        if (type === "middle") {
            return new Enemy({
                width: 46,
                height: 60,
                x: random(0, Map.width - 46),
                y: 0,
                img: "images/mid_fly.png",
                speed: 3
            });
        }
        if (type === "big") {
            return new Enemy({
                width: 110,
                height: 164,
                x: random(0, Map.width - 110),
                y: 0,
                img: "images/big_fly.png",
                speed: 2
            })
        }
    }
};